import {fileURLToPath, URL} from 'node:url'

import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'

// https://vite.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        vueJsx(),
        vueDevTools(),
    ],
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url))
        },
    },
    server: {
        host: true,
        open: true,
        port: 9527,
        proxy: {
            '/api': {
                // target: 'http://localhost:8000', // 服务器开发环境
                // target: 'http://krado.cn:18010', // 服务器测试环境
                // target: 'http://192.168.31.81:8080', // 本地测试 mac
                target: 'http://192.168.31.165:8000', // 本地测试 win
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ''),
            },
        },
        watch: {
            usePolling: true,
        },
    },
    build: {
        outDir: 'dist',
        assetsInlineLimit: 0,
        emptyOutDir: true,
        rollupOptions: {
            input: {
                main: fileURLToPath(new URL('./index.html', import.meta.url)),
            },
            output: {
                entryFileNames: 'js/[name]-[hash].js',
                chunkFileNames: 'js/[name]-[hash].js',
                assetFileNames: (assetsInfo) => {
                    const imageEnd = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'ico']
                    if (assetsInfo.names.includes('.css')) return 'css/[name]-[hash].css'
                    if (imageEnd.includes(assetsInfo.names[0].split('.').pop() as string))
                        return 'images/[name]-[hash].[ext]'
                    else return 'assets/[name]-[hash].[ext]'
                },
            },
        },
    },
})
